//
//  LHStatusPictureView.swift
//  sina_weibo
//
//  Created by 刘刘海强 on 16/5/16.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit

//每项之间的距离
let itemMargin: CGFloat = 5
//每项的宽度
let itemWidth = (ScreenWidth - 2 * StatusTableViewCellMargin - 2 * itemMargin) / 3

let LHStatusPictureViewCellIdentifier = "LHStatusPictureViewCellIdentifier"




class LHStatusPictureView: UICollectionView , UICollectionViewDataSource{

    var picUrl: [LHStatusPictureInfo]? {
        didSet {
            
            //定义一个函数计算pictureView的尺寸
            let pictureViewSize = getsize((picUrl?.count)!)
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pictureViewSize)
            }
            reloadData()
        }
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        //设置间距
        flowLayout.minimumInteritemSpacing = itemMargin
        flowLayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
        
        dataSource = self
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        registerClass(LHStatusPictureViewCell.self, forCellWithReuseIdentifier: LHStatusPictureViewCellIdentifier)
        
    }
    
    
    func getsize(count: Int) -> CGSize {
        
        let cols = count > 3 ? 3 : count
        let rows = (count - 1) / 3 + 1
        
        let width = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        let height = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        
        return CGSize(width: width, height: height)
    }
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (picUrl?.count) ?? 0
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LHStatusPictureViewCellIdentifier, forIndexPath: indexPath) as! LHStatusPictureViewCell
        cell.backgroundColor = UIColor.redColor()
        cell.picInfo = picUrl![indexPath.item]
        return cell
    }

}




class LHStatusPictureViewCell: UICollectionViewCell {

    var picInfo: LHStatusPictureInfo? {
        didSet {
            
            guard let url = picInfo?.thumbnail_pic else {
                return
            }

            imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            
            gifImageView.hidden = !url.hasSuffix(".gif")
        }
    }
    
    private lazy var imageView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}